/**
 * 给定一个没有重复的数字的序列，返回其所有可能的全排列
 * 示例:
 *  输入: [1, 2, 3]
 *  输出: [
 *      [1, 2, 3],
 *      [1, 3, 2],
 *      [2, 1, 3],
 *      [2, 3, 1],
 *      [3, 1, 2],
 *      [3, 2, 1],
 *  ]
*/
function permutation (nums) {
    if (nums.length === 1) { return [nums] }
    // 递归处理
    const result = [];
    for (let i = 0; i < nums.length; i++) {
        const first = nums[i];
        const rest = permutation(nums.slice(0, i).concat(nums.slice(i + 1)));
        for (let j = 0; j < rest.length; j++) {
            result.push([first].concat(rest[j]));
        }
    }
    return result;
}

console.log(permutation([1, 2, 3, 4]));